package com.leenmvc.core.dao.sqlServer.connect;

import com.github.pagehelper.PageInterceptor;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 数据源管理(配置多数据源) sqlServer
 */
//@Configuration
public class SqlServerDatasourceConfiguration {

    private final static Logger logger = LoggerFactory.getLogger(SqlServerDatasourceConfiguration.class);

    /**
     * 测试工程数据库
     *
     * @return
     */
    @Bean(name = "dataSource_db_weight")
    @Qualifier(value = "dataSource_db_weight")
    @ConfigurationProperties(prefix = "c3p3")
    public DataSource dataSourceC() {
        ComboPooledDataSource dataSource = DataSourceBuilder.create().type(ComboPooledDataSource.class).build();
        return dataSource;
    }

    /**
     * 为主数据源创建 sqlSessionFactory
     *
     * @return
     */
    @Bean(name = "sqlSessionFactory_db_weight")
    public SqlSessionFactory dataSourceSqlSessionFactoryWeight(org.apache.ibatis.session.Configuration config) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = null;
        try {
            // 实例SessionFactory
            sqlSessionFactoryBean = new SqlSessionFactoryBean();

            // 配置数据源
            sqlSessionFactoryBean.setDataSource(dataSourceC());

            // 加载MyBatis配置文件
            PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

            // 能加载多个，所以可以配置通配符(如：classpath*:mapper/**/*.xml)
            sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath*:mapper/weight/**/*.xml"));

            // 配置mybatis的config文件(我目前用不上)
//            sqlSessionFactoryBean.setConfiguration(config);

            //分页插件
            Interceptor interceptor = new PageInterceptor();
            Properties properties = new Properties();
            //数据库
            properties.setProperty("helperDialect", "sqlserver2012");
            //是否分页合理化
            properties.setProperty("reasonable", "false");

            interceptor.setProperties(properties);

            sqlSessionFactoryBean.setPlugins(new Interceptor[] {interceptor});

        } catch (Exception e) {
            logger.error("创建SqlSession连接工厂错误：{}", e);
        }
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    @ConfigurationProperties(prefix = "mybatis.configuration")
    public org.apache.ibatis.session.Configuration globalConfiguration() {
        return new org.apache.ibatis.session.Configuration();
    }

}
